package com.asman.umeng;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.util.Log;

import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

/**
 * Created by wangfei on 17/9/14.
 */
public class RNUMConfigure {
    public static void init(Context context, String appkey, String channel, int type, String secret, Boolean isDebug) {
        initRN("react-native", "6.0");
        UMConfigure.setLogEnabled(isDebug);
        MobclickAgent.setDebugMode(isDebug);
        UMConfigure.init(context, appkey, channel, type, secret);
//        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);//自动统计Activity
        UMConfigure.setProcessEvent(true);//支持在子线程统计自定义事件
        Log.d("device_id", getDeviceInfo(context));
    }

    @TargetApi(VERSION_CODES.KITKAT)
    private static void initRN(String v, String t) {
        Method method = null;
        try {
            Class<?> config = Class.forName("com.umeng.commonsdk.UMConfigure");
            method = config.getDeclaredMethod("setWraperType", String.class, String.class);
            method.setAccessible(true);
            method.invoke(null, v, t);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    static String getDeviceInfo(Context context) {
        String[] aaa = UMConfigure.getTestDeviceInfo(context);
        return "{\"device_id\":\"" + aaa[0] + "\",\"mac\":\"" + aaa[1] + "\"}";
    }
}
